function Request(baseUrl, query) {
    this.baseUrl = baseUrl;
    this.query = null;

    if ( query ) {
        this.query = query;
    }
}

Object.defineProperty(Request.prototype, 'url', {
    get: function() {
        return Request.makeUrl(this.baseUrl, this.query);
    }
});

Request.prototype.getJSON = function(callback, error) {
    getJSON(this.url, callback, error);
}

Request.prototype.postJSON = function(data, callback, error) {
    postJSON(this.url, data, callback, error);
}

Request.makeUrl = function(baseUrl, query) {
    if ( !query ) {
        return baseUrl;
    }

    var queryString = Request.makeQueryString(query);
    if ( queryString.length == 0 ) {
        return baseUrl;
    }

    return baseUrl + '?' + queryString;
}

Request.extractBaseUrl = function(href) {
    var queryPos = href.indexOf('?');
    if ( queryPos == -1 ) {
        return href;
    }

    return href.slice(0, queryPos);
}

Request.makeQueryString = function(parameters) {
    parameterList = [];
    for ( var key in parameters ) {
        var value = parameters[key];

        parameterList.push(key + '=' + value);
    }

    return parameterList.join('&');
}

Request.parseQueryString = function(queryString) {
    queryString = queryString.substring(1);
    if ( queryString == null || queryString == '' ) {
        return {};
    }

    var parameterList = queryString.split('&');
    var parameters = {};
    for ( parameterIndex in parameterList ) {
        var parameter = parameterList[parameterIndex];

        var keyValue = parameter.split('=');
        var key = keyValue[0];
        var value = keyValue[1];

        parameters[key] = value;
    }

    return parameters;
}

function Response() {
}

Response.prototype.redirectTo = function(request) {
    location.href = request.url;
}

var Application = {
    showPage: function(page) {
        var queryString = window.location.search;
        var baseUrl = Request.extractBaseUrl(location.href);
        var query = Request.parseQueryString(queryString);
        var request = new Request(baseUrl, query);
        var response = new Response();

        page.request = request;
        page.response = response;

        if ( 'onInitialize' in page ) {
            page.onInitialize();
        }
        if ( 'onShow' in page ) {
            page.onShow();
        }
    }
};

function postJSON(url, data, callback, error) {
    $.ajax({
        url: url,
        type: 'post',
        contentType: 'application/json',
        data: JSON.stringify(data),
        dataType: 'json',
        success: function(data) {
            callback(data);
        },
        error: function(req, message, e) {
            console.log(message);
            console.log(e);

            if ( error ) {
                error(req, message, e);
            }
        }
    });
}

function getJSON(url, callback, error) {
    $.ajax({
        url: url,
        type: 'get',
        contentType: 'application/json',
        dataType: 'json',
        success: function(data) {
            callback(data);
        },
        error: function(req, message, e) {
            console.log(message);
            console.log(e);

            if ( error ) {
                error(req, message, e);
            }
        }
    });
}

function toJSON(object) {
    return JSON.stringify(object);
}

function fromJSON(data) {
    if ( !data ) {
        return undefined;
    }

    return $.parseJSON(data);
}

function redirectTo(url) {
    window.location.href = url;
}

function disableAll(e) {
    e.preventDefault();
    e.stopPropagation();
}

//显示错误信息
function showError(e){

    $.toast("失败!!");
    console.log(e);
}

function warnEmpty(msg,warn){
    if (msg==''){

        $.alert(warn+'不能为空');
        return true;
    }else{
        return false;
    }
}

function isTel(msg){
    var re = /^[0-9]{11}$/

    return re.test(msg);
}

function calcPrice(crowdfundingMap, crowdfundings, cartItems) {
    var price = 0;
    var len=cartItems.length;

    for(var i=0;i<len;i++){
        var flag=0;
        var crowdfundingId = cartItems[i].item.crowdfunding.id;
        var _crowdfunding = crowdfundingMap[crowdfundingId];

        var len_rebounds=_crowdfunding.rebounds.length;

        for(var j=len_rebounds-1;(j>=0)&&(flag==0);j--){
            if(cartItems[i].count>=_crowdfunding.rebounds[j].count){
                price +=  (_crowdfunding.discount-_crowdfunding.rebounds[j].price)*cartItems[i].count;
                flag=1;
            }
        }
        if(flag==0){
            price += (_crowdfunding.discount)*cartItems[i].count;
        }
    }
    return price;
}
